<?php

namespace Topxia\Service\Kuke\Dao\Impl;

use Topxia\Service\Common\BaseDao;
use Topxia\Service\Kuke\Dao\ActiveMaDao;

class ActiveMaDaoImpl extends BaseDao implements ActiveMaDao
{
    protected $table = 'activema';

	public function getActiveMa($conditions,$start,$limit)
	{
        $sql = "SELECT * FROM {$this->table} where 1=1";
		if(isset($conditions['activema'])){
			$sql .= ' and activema = \''.$conditions['activema'].'\'';
		}elseif(isset($conditions['mac'])){
			$sql .= ' and mac = \''.$conditions['mac'].'\'';
		}else{
			if(isset($conditions['hezuoshang']))
				$sql .= ' and hezuoshang = \''.$conditions['hezuoshang'].'\'';
			if(isset($conditions['starttime']))
				$sql .= ' and starttime >= \''.$conditions['starttime'].'\'';
			if(isset($conditions['endtime']))
				$sql .= ' and starttime <= \''.$conditions['endtime'].'\'';
			if(isset($conditions['area']))
				$sql .= ' and area like "%'.$conditions['area'].'"';
		}
		$sql .= " order by endtime desc, id desc limit $start,$limit";
        return $this->getConnection()->fetchAll($sql) ? : null;
	}

	public function buildding($conditions)
	{
		$sql = ' select build('.$conditions['num'].', '.$conditions['stime'].', \''.$conditions['hezuoshang'].'\');';
		return  $this->getConnection()->fetchColumn($sql);
	}

	public function getActiveMaCount($conditions)
	{
		$sql = " select count(id) from {$this->table} as num where 1=1";

		if(isset($conditions['hezuoshang']))
			$sql .= ' and hezuoshang = \''.$conditions['hezuoshang'].'\'';
		if(isset($conditions['starttime']))
			$sql .= ' and starttime > \''.$conditions['starttime'].'\'';
		if(isset($conditions['endtime']))
			$sql .= ' and endtime < \''.$conditions['endtime'].'\'';
		if(isset($conditions['area']))
			$sql .= ' and area like "%'.$conditions['area'].'"';
		if(isset($conditions['status']))
			$sql .= ' and status = "'.$conditions['status'].'"';

		return  $this->getConnection()->fetchColumn($sql);
	}

	public function set($conditions)
	{
		$sql = "update {$this->table} set validity = ".$conditions['validity']." where id in (0";
		foreach($conditions['ids'] as $id)
		{
			$sql .=",$id";
		}
		$sql .= ")";
		return  $this->getConnection()->exec($sql);
	}

	public function reset($conditions)
	{
		$sql = "update {$this->table} set status=0,starttime=null,endtime=null where id in (0";
		foreach($conditions['ids'] as $id)
		{
			$sql .=",$id";
		}
		$sql .= ")";
		return  $this->getConnection()->exec($sql);
	}

	public function activing($conditions)
	{
		if(isset($conditions['activema']))
			$sql = "select status,validity from {$this->table} where activema = '{$conditions['activema']}' limit 1";
		else
			return array("status"=>"err","message"=>"请输入激活码", 'id'=>1);

		$res = $this->getConnection()->fetch($sql);
		if($res == null){
			return array("status"=>"err","message"=>"该激活码不存在", 'id'=>2);
		}elseif($res['status']== 2){
			return array("status"=>"err","message"=>"该激活码已到期", 'id'=>5);
		}elseif($res['status'] && $res['status'] != 3){
			return array("status"=>"err","message"=>"该激活码已被激活", 'id'=>3);
		}

		$sql = "select machine,mac,xingming,mobile,qq,area from activetry where machine = '{$conditions['machine']}' and mac = '{$conditions['mac']}' limit 1";
		$userinfo = $this->getConnection()->fetch($sql);
		$conditions = array_merge($conditions,$userinfo);

		$sql = "update {$this->table} set";
		if(isset($conditions['machine']))
			$sql .=" machine = '".$conditions['machine']."',";
		if(isset($conditions['mac']))
			$sql .=" mac = '".$conditions['mac']."',";
		if(isset($conditions['xingming']))
			$sql .=" xingming = '".$conditions['xingming']."',";
		if(isset($conditions['mobile']))
			$sql .=" mobile = '".$conditions['mobile']."',";
		if(isset($conditions['qq']))
			$sql .=" qq = '".$conditions['qq']."',";
		if(isset($conditions['area']))
			$sql .="area = '".$conditions['area']."',";
		$sql .= " status = 1, starttime='".strtotime("now")."',endtime='".strtotime("+".$res['validity']."month")."' ";
		$sql .= "where activema='".$conditions['activema']."'";
		$res = $this->getConnection()->exec($sql);
		if($res > 0){
			return array("status"=>"ok","message"=>"激活成功", 'id'=>0);
		}else{
			return array("status"=>"err","message"=>"激活失败", 'id'=>4);
		}
	}

	public function getstatus($activema)
	{
		$sql = "select status, endtime from {$this->table} where activema = '$activema' limit 1";
		$res = $this->getConnection()->fetch($sql);
		
		if(!$res['status'] || $res['status'] == 3){
			return array("status"=>"err","message"=>"该激活码未激活", 'id'=>2);
		}elseif($res['status'] == 2){
			return array("status"=>"err","message"=>"该激活码已到期", 'id'=>1);
		}elseif(($res['endtime']+0) < time()){
			$sql = "update {$this->table} set status = 2 where activema = '$activema'";
			$this->getConnection()->exec($sql);
			return array("status"=>"err","message"=>"该激活码已到期", 'id'=>1);
			
		}else{
			return array("status"=>"ok","message"=>"该激活码正常使用中", 'id'=>0);
		}
	}

	public function gettrystatus($conditions)
	{
		$sql = "select id,xingming from activetry where machine = '{$conditions['machine']}' and mac = '{$conditions['mac']}' limit 1";
		//$sql = "select id,xingming from activetry where machine = '{$conditions['machine']}' limit 1";
		$res = $this->getConnection()->fetch($sql);
		if($res){
			return array("status"=>"err","message"=>"已试用", 'id'=>1, 'username'=>$res['xingming']);
		}else{
			return array("status"=>"ok","message"=>"未试用", 'id'=>0);
		}
	}

	public function trying($conditions){

//		$sql="select id from user where nickname = '{$conditions['xingming']}'";
//		$userid = $this->getConnection()->fetchColumn($sql);
//		if($userid){
//			return array("status"=>"err","message"=>"重复试用","id"=>2);
//		}

		//$sql="select id from activetry where mac = '{$conditions['mac']}' and machine = '{$conditions['machine']}'";
		$sql="select id from activetry where machine = '{$conditions['machine']}'";
		$id = $this->getConnection()->fetchColumn($sql);
		if(!$id){
			$sql  = "insert into activetry (machine,mac,starttime,xingming,mobile,qq,area)";
			$sql .= "value('{$conditions['machine']}','{$conditions['mac']}','".time();
			$sql .= "','{$conditions['xingming']}','{$conditions['mobile']}','{$conditions['qq']}','{$conditions['area']}')";
		}else{
			$sql  = "update activetry set xingming='{$conditions['xingming']}',mobile='{$conditions['mobile']}',starttime= '";
			$sql .= time()."',qq='{$conditions['qq']}',area='{$conditions['area']}',mac='{$conditions['mac']}'";
			$sql .= " where id = $id";
		}

		$success = $this->getConnection()->exec($sql);
		if($success)
			return array("status"=>"ok","message"=>"试用成功", 'id'=>0);
		else
			return array("status"=>"err","message"=>"试用失败", 'id'=>1);
	}
	
	public function getactivemaByMachine($conditions){
		$sql="select activema from {$this->table} where mac = '{$conditions['mac']}' and machine = '{$conditions['machine']}'";
		return $this->getConnection()->fetchColumn($sql);
	}

	public function gettrylist($conditions,$start,$limit){
		$sql = "SELECT * FROM activetry where 1=1";
		if(isset($conditions['machine'])){
			$sql .= ' and machine = \''.$conditions['machine'].'\'';
		}elseif(isset($conditions['mac'])){
			$sql .= ' and mac = \''.$conditions['mac'].'\'';
		}else{
			if(isset($conditions['starttime']))
				$sql .= ' and starttime >= \''.$conditions['starttime'].'\'';
			if(isset($conditions['endtime']))
				$sql .= ' and starttime <= \''.$conditions['endtime'].'\'';
			if(isset($conditions['area']))
				$sql .= ' and area like "%'.$conditions['area'].'%"';
		}
		$sql .= " order by id desc limit $start,$limit";
        return $this->getConnection()->fetchAll($sql) ? : null;
	}

	public function gettryCount($conditions)
	{
		$sql = " select count(id) from activetry as num where 1=1";

		if(isset($conditions['starttime']))
			$sql .= ' and starttime > \''.$conditions['starttime'].'\'';
		if(isset($conditions['endtime']))
			$sql .= ' and starttime < \''.$conditions['endtime'].'\'';
		if(isset($conditions['area']))
			$sql .= ' and area like "%'.$conditions['area'].'%"';
		return  $this->getConnection()->fetchColumn($sql);
	}

	//曹乾
	public function select()
	{
		$sql = "SELECT * FROM {$this->table} WHERE status= 0 and (mac = '' or mac is null) and (machine = '' or machine is null) LIMIT 1";
		return $this->getConnection()->fetchAll($sql);

	}

	public function updateActiveMa($id)
	{
		$sql = "UPDATE {$this->table} SET status=3 where id=".$id;
		return $this->getConnection()->exec($sql);
	}
}